<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    ChinaWanderer
 * @package     Backend
 * @subpackage  Controller
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: RoleResourceController.php 1047 2011-05-26 09:16:22Z chen.qiao $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
*/

/**
 * role resource controller for backend
 * 
 * view,delete,search,sort,edit role resource
 * 
 * @package     Backend
 * @subpackage  Controller
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: RoleResourceController.php 1047 2011-05-26 09:16:22Z chen.qiao $$
 */

class Default_RoleResourceController extends Evebit_Controller_Action_Backend {
	
	/**
	 * @var $_controllerUrl string
	 */
	private $_controllerUrl = 'role-resource';
	/**
	 * @var $_moduleUrl string
	 */
	private $_moduleUrl = 'default';
	
	/**
	 * default action of the role resource controller
	 * 
	 * view the role resource
	 * 
	 */
	public function indexAction() {
		$session = $this->getSession();
		unset($session->grids['filters']);
		$session->initGrid('RoleResource', 
			array(
				'id'=> array(), 
			    'module'=> array('fields'=> 'module', 'type'=> 'string'),
				'controller'=> array('fields'=> 'controller', 'type'=> 'string'),
				'action'=> array('fields'=> 'action', 'type'=> 'string'),
				'params'=> array('fields'=> 'params', 'type'=> 'string'),
				'scope'=> array('fields'=> 'scope', 'type'=> 'string')	
				),		
			array(
				'id'=> 'id', 
			    'module'=> 'module',
				'controller'=> 'controller',
				'action'=> 'action',
				'params'=> 'params',
				'scope'=> 'scope'
				),
			array('size'=> 10, 'sort'=> 'id', 'order'=> 'desc'));
		$permission = new Default_Model_Entity_Resource();
		$collection = $permission->getCollection();
		$adapter = new Evebit_Model_Paginator_Adapter($collection);
		$paginator = new Zend_Paginator($adapter);
		$session->applyCollection($collection, $paginator);
		$this->view->collection = $collection;
		$this->view->paginator = $paginator;
		$this->view->state = $session->getGridState();
	}
	
	/**
	 * reset action of the role resource controller
	 * 
	 * reset the role resource
	 * 
	 */
	public function resetAction() {
		$this->getSession()->resetGrid();
		$this->_forward("index", $this->_controllerUrl);
	}
	
	/**
	 * add action of the role resource controller
	 * 
	 * add a role resource
	 * 
	 */
	public function addAction() {
		$this->_forward('edit', null, null, array('id'=> 0));
	}
	
	/**
	 * edit action of the role resource controller
	 * 
	 * edit a role resource,if the role resource is exist,show the role resource,
	 * else show a blank page to add a role resource
	 * 
	 */
	public function editAction() {
		$id = $this->getRequest()->getParam('id');
		$permission = new Default_Model_Entity_Resource();
		if ($id) {
			$permission->load($id);
		}
		$this->view->permission = $permission;
	}
	
	/**
	 * saveEdit action of the role resource controller
	 * 
	 * when an administrator edited the role resource,save it
	 * 
	 */
	public function saveEditAction() {
		$permission = new Default_Model_Entity_Resource();
		$data = $this->getRequest()->getPost('data');
		$permission->addData($data);
		try {
			$result=$permission->save();
		} catch (Exception $e) {
			$this->getSession()->addError($this->__('Register failed'));
			return $this->_forward('edit', $this->_controllerUrl, $this->_moduleUrl, array("id"=> $data["id"]));
		}
		$app = Evebit_Application::app();
		$this->getSession()->addSuccess($this->__('Save Success'));
		$this->_redirect($this->_controllerUrl.'/index');
	}
	
    /**
	 * delete action of the role resource controller
	 * 
	 * delete one or more role resource
	 * 
	 */
	public function deleteAction() {
		$ids = $this->getRequest()->getParam('id');
		if (count($ids) > 0) {
			foreach ($ids as $id) {
				try {
					$permission = new Default_Model_Entity_Resource();
					$permission->load($id);
					if ($permission->getId() != $id) {
						throw new Exception('');
					}
					$permission->delete();
				} catch (Exception $e) {
					$this->getSession()->addError(
						$this->__('RoleResouce not found or deleted failed'));
				}
			}
			$this->getSession()->addSuccess($this->__('Delete Success'));
		} else {
			$this->getSession()->addNotice($this->__('Please select a record'));
		}
		$this->_redirect($this->_controllerUrl);
	}

}
